package com.weijian.controller;

import com.weijian.common.ApiResponse;
import com.weijian.dal.dto.cart.AddCartRequest;
import com.weijian.dal.dto.cart.CartItemDTO;
import com.weijian.dal.dto.cart.UpdateCartRequest;
import com.weijian.service.CartService;
import com.weijian.util.SessionUserUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车控制器
 *
 * @author weijian
 */
@Tag(name = "购物车管理")
@RestController
@RequestMapping("/api/cart")
@RequiredArgsConstructor
@Validated
public class CartController {

    private final CartService cartService;

    @Operation(summary = "添加商品到购物车", description = "将指定课程添加到用户购物车")
    @PostMapping("/add")
    public ApiResponse<CartItemDTO> addToCart(
            @Valid @RequestBody AddCartRequest request,
            HttpSession session) {
        Long userId = SessionUserUtil.getCurrentUserId();
        CartItemDTO cartItem = cartService.addToCart(userId, request);
        return ApiResponse.success(cartItem);
    }

    @Operation(summary = "查询用户购物车列表", description = "获取当前登录用户的购物车商品列表")
    @GetMapping("/list")
    public ApiResponse<List<CartItemDTO>> getCartList(HttpSession session) {
        Long userId = SessionUserUtil.getCurrentUserId();
        List<CartItemDTO> cartList = cartService.getCartList(userId);
        return ApiResponse.success(cartList);
    }

    @Operation(summary = "更新购物车商品", description = "更新购物车中指定商品的数量")
    @PutMapping("/update")
    public ApiResponse<CartItemDTO> updateCartItem(
            @Valid @RequestBody UpdateCartRequest request,
            HttpSession session) {
        Long userId = SessionUserUtil.getCurrentUserId();
        CartItemDTO cartItem = cartService.updateCartItem(userId, request);
        return ApiResponse.success(cartItem);
    }

    @Operation(summary = "删除购物车商品", description = "从购物车中移除指定课程")
    @DeleteMapping("/remove/{courseId}")
    public ApiResponse<Void> removeFromCart(
            @Parameter(description = "课程ID") @PathVariable Long courseId,
            HttpSession session) {
        Long userId = SessionUserUtil.getCurrentUserId();
        cartService.removeFromCart(userId, courseId);
        return ApiResponse.success();
    }

    @Operation(summary = "清空购物车", description = "清空当前用户的整个购物车")
    @DeleteMapping("/clear")
    public ApiResponse<Void> clearCart(HttpSession session) {
        Long userId = SessionUserUtil.getCurrentUserId();
        cartService.clearCart(userId);
        return ApiResponse.success();
    }

    @Operation(summary = "选择/取消选择购物车商品", description = "设置购物车商品的选择状态，用于批量结算")
    @PutMapping("/select/{cartItemId}")
    public ApiResponse<CartItemDTO> selectCartItem(
            @Parameter(description = "购物车商品ID") @PathVariable Long cartItemId,
            @Parameter(description = "是否选中") @RequestParam Boolean selected,
            HttpSession session) {
        Long userId = SessionUserUtil.getCurrentUserId();
        CartItemDTO cartItem = cartService.selectCartItem(userId, cartItemId, selected);
        return ApiResponse.success(cartItem);
    }

    @Operation(summary = "获取选中的购物车商品", description = "获取当前用户购物车中已选中的商品列表，用于结算")
    @GetMapping("/selected")
    public ApiResponse<List<CartItemDTO>> getSelectedItems(HttpSession session) {
        Long userId = SessionUserUtil.getCurrentUserId();
        List<CartItemDTO> selectedItems = cartService.getSelectedItems(userId);
        return ApiResponse.success(selectedItems);
    }

    @Operation(summary = "获取购物车商品总数", description = "获取当前用户购物车中的商品总数量")
    @GetMapping("/count")
    public ApiResponse<Integer> getCartItemCount(HttpSession session) {
        Long userId = SessionUserUtil.getCurrentUserId();
        int count = cartService.getCartItemCount(userId);
        return ApiResponse.success(count);
    }
}